package great.easychat.help.uinque;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.util.Log;

import java.util.Random;

/**
 * 根据设备名称以及设备型号和驱动长度rom仿造一个IMEI
 * we make this look like a valid IMEI
 * 1.每次先检测文件里面是否生成了一个加密的文件（）
 */
public class UUIDUtils {
    private final static String TAG = "UUID";

    private static UUIDUtils uuidUtils;

    private UUIDUtils() {

    }

    public static synchronized UUIDUtils getInstance() {
        if (uuidUtils == null) {
            uuidUtils = new UUIDUtils();
        }
        return uuidUtils;
    }


    /**
     * @return
     */
    public String getUniqueID(Context context) {
        return getUniqueID(context, null);
    }

    @SuppressLint({"MissingPermission", "HardwareIds"})
    public String getUniqueID(Context context, String pageName) {
        String serial = null;
        String wlanMac = null;
        String imei = null; //IMEI码
        String uuid = null;

        //we make this look like a valid IMEI
        String deviceId = pageName +
                Build.BOARD.length() % 10 + Build.BRAND.length() % 10
                + Build.CPU_ABI.length() % 10 + Build.DEVICE.length() % 10
                + Build.DISPLAY.length() % 10 + Build.HOST.length() % 10
                + Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10
                + Build.MODEL.length() % 10 + Build.PRODUCT.length() % 10
                + Build.TAGS.length() % 10 + Build.TYPE.length() % 10
                + Build.USER.length() % 10; // 13 digits;

//        Log.d("TAG", "deviceId ===》" + deviceId);

        if (context == null) {
            return "context == null";
        }

        //通过反射拿到serial去生成一个串号
        try {
            //API>=9 使用serial号
            serial = Build.class.getField("SERIAL").get(null).toString();
        } catch (Exception exception) {
            //serial需要一个初始化        serial = "serial"; // 随便一个初始化
        }
        //通过系统权限获取手机信息获取到一个IMEI随即串
        try {
            //待权限开启之后才可以获取到设备的IMEI唯一识别码 如果不开启权限的时候 IMEI == null
            TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            if (telephonyManager != null) {
                imei = telephonyManager.getDeviceId();
            }
//            Log.d("TAG", "imei ===》" + imei);
        } catch (Exception e) {
//            Log.d("TAG", "设备IMEI获取为null");
        }

        //获取到网络mac地址
        try {
            WifiManager wm = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            if (wm != null) {
                wlanMac = wm.getConnectionInfo().getMacAddress();
            }
//            Log.d("TAG", "wlanMac ===》" + wlanMac);
        } catch (Exception e) {
//            Log.d("TAG", "网络设备获取mac地址为空");
        }
        uuid = Encryption.md5(deviceId + serial + wlanMac + imei);

        Log.d(TAG, "uuid ===》" + uuid);
        return uuid;
    }
}
